package manage.controller;

import manage.utils.ControllerUtils;
import net.lcheng.commons.utils.PageList;
import net.lcheng.model.PProduct;
import net.lcheng.service.PProductService;
import net.lcheng.service.SysPermissionsService;
import net.lcheng.vo.PProductModel;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.Date;
import java.util.UUID;

/**
 * Created by zwr on 2016/8/30.
 */
@Controller
public class ProductController  {
    @Autowired
    private SysPermissionsService permissionsService;
    @Autowired
    private PProductService pProductService;
    private final int PAGESIZE = 10;

    @RequiresPermissions("product:product_list")
    @RequestMapping(value = "/product/product_list",method = RequestMethod.GET)
    public String list(Model model, @RequestParam(defaultValue = "1", required = false) int page, HttpServletRequest request){
        ControllerUtils.common(model,  permissionsService,"product:product_list" );
        String productName = request.getParameter("productName");
        String stime = request.getParameter("stime");
        String etime = request.getParameter("etime");
        Date sdate = null;
        Date edate = null;
        try {
            sdate = java.sql.Date.valueOf(stime);
            edate = java.sql.Date.valueOf(etime);
        } catch (Exception ex) {
        }
        /**商品查询*/
        PageList<PProduct>pageList = pProductService.setPProductList(page, PAGESIZE, productName, sdate, edate);
        model.addAttribute("productName", productName);
        model.addAttribute("stime", stime);
        model.addAttribute("etime", etime);
        model.addAttribute("list", pageList.list);
        model.addAttribute("pageIndex", page);
        model.addAttribute("totalPage", pageList.totalPage);
        return "product_list";
    }
    /**
     * 删除Get
     */
    @RequiresPermissions("product:product_delete")
    @RequestMapping(value = "/product/product_delete", method = RequestMethod.GET)
    public String categoryDeleteGet(Model model, @RequestParam String id) {
        int returnResult = pProductService.dateStatus(id);
        if (returnResult > 0) {
            model.addAttribute("error", "修改成功");
        } else model.addAttribute("error", "系统异常");
        return "redirect:/product/product_list";
    }
    /***
     * 下架经销商品
     * @param id
     * @return
     */
    @ResponseBody
    @RequiresPermissions("product:status")
    @RequestMapping(value = "/product/status", method = RequestMethod.POST)
    public int Status(Model model,@RequestParam String id,@RequestParam Integer status) {
        ControllerUtils.common(model,  permissionsService,"product:status");
        return pProductService.getPProductByStatus(id,status);
    }
    /***
     * 添加商品-GET
     * @param model
     * @return
     */
    @RequiresPermissions("product:add")
    @RequestMapping(value="/product/add",method = RequestMethod.GET)
    public String add(Model model){
        ControllerUtils.common(model,permissionsService,"product:add");
        if(!model.containsAttribute("PProductModel")){
            model.addAttribute("PProductModel",new PProductModel());
        }
        return "product_add";
    }

    /***
     * 添加商品-POST
     * @param model
     * @param pProductModel
     * @param result
     * @return
     */
    @RequiresPermissions("product:add")
    @RequestMapping(value="/product/add",method = RequestMethod.POST)
    public String addPost(Model model, @Valid @ModelAttribute("PProductModel") PProductModel pProductModel, BindingResult result){
        if(result.hasErrors()){
            ControllerUtils.common(model,permissionsService,"product:add");
            return "product_add";
        }
        PProduct pProduct= new PProduct();
        pProduct.setProductId(UUID.randomUUID().toString());
        pProduct.setProductName(pProductModel.getProductName());
        pProduct.setProductMainImage(pProductModel.getProductMainImage());
        pProduct.setStatus(1);
        /**String类型转换为BigDecimal类型*/
        String salePrice = pProductModel.getSalePrice();
        BigDecimal salePriceBigDecimal = new BigDecimal(salePrice);
        pProduct.setSalePrice(salePriceBigDecimal);

        pProduct.setCreateOn(new Date());
        pProduct.setProductDescription(pProductModel.getProductDescription());
        int returnResult = pProductService.addPProduct(pProduct);
        if (returnResult > 0) {
            return "redirect:/product/product_list";
        }
        return "redirect:/product/product_list";
    }
}
